package com.pdsu.alumni.service.impl;

import cn.hutool.core.lang.UUID;
import com.pdsu.alumni.config.RabbitMQConfig;
import com.pdsu.alumni.pojo.Mail;
import com.pdsu.alumni.pojo.MqLog;
import com.pdsu.alumni.service.IMqLogService;
import com.pdsu.alumni.service.IProduceService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: alumni
 * @description: 生产服务类
 * @author: Acerola
 * @create: 2021-12-13 23:24
 **/
@Service
public class ProduceServiceImpl implements IProduceService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IMqLogService mqLogService;

    @Override
    public boolean send(Mail mail) {
        //创建uuid
        String msgId = UUID.randomUUID().toString().replaceAll("-", "");
        mail.setMsgId(msgId);
        //消息入库
        MqLog mqLog = new MqLog(msgId, mail, RabbitMQConfig.MAIL_EXCHANGE_NAME, RabbitMQConfig.MAIL_ROUTING_KEY_NAME);
        mqLogService.save(mqLog);
        //发送消息到rabbitMQ
        CorrelationData correlationData = new CorrelationData(msgId);
        rabbitTemplate.convertAndSend(RabbitMQConfig.MAIL_EXCHANGE_NAME, RabbitMQConfig.MAIL_ROUTING_KEY_NAME, mail, correlationData);
        return true;
    }
}